package com.openroom.ui;

import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.ComponentMouseButtonListener;
import org.apache.pivot.wtk.Cursor;
import org.apache.pivot.wtk.ImageView;
import org.apache.pivot.wtk.Mouse.Button;
import org.apache.pivot.wtk.media.Image;

import com.openroom.model.ModelLocator;
import com.openroom.model.ModelLocator.ModeFlag.SelectMode;
import com.openroom.model.Photograph;

/**
 * Custom ImageView which will be paired with a custom ImageView skin.
 */
public class FocusableImageView extends ImageView {

    public static final String IMAGE_DATA_KEY = "IMAGE";

    private final class MouseButtonListener implements ComponentMouseButtonListener {
        @Override
        public boolean mouseDown(Component component,
                Button button, int x, int y) {
            return false;
        }

        @Override
        public boolean mouseUp(Component component, Button button,
                int x, int y) {
            return false;
        }

        @Override
        public boolean mouseClick(Component component,
                Button button, int x, int y, int count) {
            FocusableImageView view = (FocusableImageView) component;
            Photograph userData = (Photograph) view.getUserData().get(IMAGE_DATA_KEY);
            if (ModelLocator.getInstance().getCurrentSelectMode().equals(SelectMode.SINGLE)) {
                ModelLocator.getInstance().setCurrentlySelectedImage(userData);
            }
            else
            {
                ModelLocator.getInstance().addToMultiselectImages(userData);
            }
            return false;
        }
    }

    public FocusableImageView() {
        this(null);
    }

    public FocusableImageView(Image image) {
        super(image);
        installSkin(FocusableImageView.class);
        setCursor(Cursor.HAND);
        getComponentMouseButtonListeners().add(new MouseButtonListener());
    }

}